package com.example.wyh.intelcheckbash.task;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wyh.intelcheckbash.LocalStorage;
import com.example.wyh.intelcheckbash.R;
import com.example.wyh.intelcheckbash.SeismicActivity;
import com.example.wyh.intelcheckbash.temptemperature.ThermometricActivity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by chen on 17/10/27.
 */

public class ListViewAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private JSONObject completeDate;
    private ArrayList<ListViewItem> listDatas;
    private OnViewCompleteListener mOnViewCompleteListener;


    public ListViewAdapter(Context context, ArrayList<ListViewItem> listDatas, JSONObject completeDate) {
        mContext = context;
        this.listDatas = listDatas;
        this.completeDate = completeDate;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    //返回当前布局的样式type
    @Override
    public int getItemViewType(int position) {
        return listDatas.get(position).type;
    }


    //返回你有多少个不同的布局
    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getCount() {
        return listDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return listDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ListViewItem listItem = listDatas.get(position);
        final int mPosition = position;
        int Type = getItemViewType(position);
        ViewHolderfirstType viewHolderfirstType = null;
        ViewHoldersecondType viewHoldersecondType = null;
        ViewHolderChaobiaoType viewHolderChaobiaoType = null;
        switch (Type) {
            case 0:
                viewHolderfirstType = new ViewHolderfirstType();
                convertView = mLayoutInflater.inflate(R.layout.list_item_ganguan, null);
                viewHolderfirstType.check_title_text_view = (TextView) convertView.findViewById(R.id.check_title_gangan);
                viewHolderfirstType.check_standard_text_view = (TextView) convertView.findViewById(R.id.check_standard_gangan_id);
                viewHolderfirstType.checkBoxPass = (CheckBox) convertView.findViewById(R.id.pass_id);
                viewHolderfirstType.checkBoxDisPass = (CheckBox) convertView.findViewById(R.id.dispass_id);
//                    viewHolderfirstType.checkFaceBugId = (CheckBox) convertView.findViewById(R.id.face_bug_id);
                viewHolderfirstType.check_title_text_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                        task_activity.jumpToCheckInfoActivity(mPosition);
                    }
                });
                try {

                    JSONArray cdjgzlb = listItem.map.get(position + "").getJSONArray("cdjgzlb");
                    String jgzmr = listItem.map.get(position + "").getString("jgzmr");
                    String correctValue = cdjgzlb.getJSONObject(0).getString("z");
                    String errorValue = cdjgzlb.getJSONObject(1).getString("z");
                    viewHolderfirstType.checkBoxPass.setText(correctValue);
                    viewHolderfirstType.checkBoxDisPass.setText(errorValue);

                    viewHolderfirstType.checkBoxPass.setChecked(jgzmr.equals("1")?cdjgzlb.getJSONObject(0).getString("sfmrx").equals("1"):false);
                    viewHolderfirstType.checkBoxDisPass.setChecked(jgzmr.equals("1")?cdjgzlb.getJSONObject(1).getString("sfmrx").equals("1"):false);

                    viewHolderfirstType.check_title_text_view.setText(listItem.map.get(position + "").getString("description"));
                    viewHolderfirstType.check_standard_text_view.setText(listItem.map.get(position + "").getString("djbz"));


                    initCheckBoxAction(viewHolderfirstType.checkBoxPass, position, "pass");
                    initCheckBoxAction(viewHolderfirstType.checkBoxDisPass, position, "dispass");
                    try {
                        if (completeDate.getJSONArray("xmdata").getJSONObject(position).getString("cdjg").equals(correctValue)) {
                            viewHolderfirstType.checkBoxPass.setChecked(true);
                            viewHolderfirstType.checkBoxDisPass.setChecked(false);
                        }
                    } catch (JSONException e) {

                    }
                    try {
                        if (completeDate.getJSONArray("xmdata").getJSONObject(position).getString("cdjg").equals(errorValue)) {
                            viewHolderfirstType.checkBoxDisPass.setChecked(true);
                            viewHolderfirstType.checkBoxPass.setChecked(false);
                        }
                    } catch (JSONException e) {

                    }
                    try {
                        if (completeDate.getJSONArray("xmdata").getJSONObject(position).getString("isqx").equals("1")) {
                            convertView.setBackgroundColor(Color.parseColor("#ffc600"));
                        }
                    }catch (JSONException e)
                    {

                    }


                } catch (Exception e) {
                    Log.e("kkkkk",e.getMessage());
                }
                //如果要复用，不同于一种样式的listView ,这样的写法是错误的,具体原因看下面第一个小哥评论的说法
                convertView.setTag(R.id.viewHolderGanguanType, viewHolderfirstType);
                break;
            case 1:
                viewHoldersecondType = new ViewHoldersecondType();
                convertView = mLayoutInflater.inflate(R.layout.list_item_celiang, null);
                viewHoldersecondType.check_title_text_view = (TextView) convertView.findViewById(R.id.check_celiang_title);
                viewHoldersecondType.check_standard_text_view = (TextView) convertView.findViewById(R.id.check_celiang_standard_id);
                viewHoldersecondType.test_seismic_btn = (Button) convertView.findViewById(R.id.test_seismic_btn_id);
                viewHoldersecondType.check_danwei_text_view = (TextView) convertView.findViewById(R.id.celiang_danwei);
//                    viewHoldersecondType.checkFaceBugId = (CheckBox) convertView.findViewById(R.id.seismic_bug_check);
                viewHoldersecondType.show_seismic_id_view = (EditText) convertView.findViewById(R.id.show_seismic_id);

                viewHoldersecondType.check_title_text_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                        task_activity.jumpToCheckInfoActivity(mPosition);
                    }
                });

                try {
                    viewHoldersecondType.check_title_text_view.setText(listItem.map.get(position + "").getString("description"));
                    viewHoldersecondType.check_standard_text_view.setText(listItem.map.get(position + "").getString("djbz"));
                    String cddw = listItem.map.get(position + "").getString("cddw");
                    if (cddw.equals("C")) {
                        cddw = " ℃";
                    }
                    viewHoldersecondType.check_danwei_text_view.setText(cddw);

                    try {
                        if(completeDate.getJSONArray("xmdata").getJSONObject(position).getString("sfycd").equals("1"))
                        {
                            viewHoldersecondType.show_seismic_id_view.setTextColor(Color.parseColor("#ff0000"));
                        }
//                        Log.e("completeDate",completeDate.toString());
                        viewHoldersecondType.show_seismic_id_view.setText(completeDate.getJSONArray("xmdata").getJSONObject(position).getString("cdjg"));
                    } catch (JSONException e) {

                    }

                    try {
                        if (completeDate.getJSONArray("xmdata").getJSONObject(position).getString("isqx").equals("1")) {
                            convertView.setBackgroundColor(Color.parseColor("#ffc600"));
                        }
                    }catch (JSONException e)
                    {

                    }


                } catch (JSONException e) {

                }
                viewHoldersecondType.test_seismic_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        String test_type_info = "";
                        try {
                            test_type_info = listItem.map.get(mPosition + "").getString("cdlx");
                        } catch (JSONException e) {

                        }
                        if (test_type_info.equals("CW")) {
                            TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                            task_activity.jumpToTestThermom(mPosition);
                        } else if (test_type_info.equals("CZ")) {
                            TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                            task_activity.jumpToTestSeismic(mPosition);
                        } else {
                            Log.e("type", "test_type_info:" + test_type_info);
                        }
                    }
                });
//                  convertView.setTag(viewHolderfirstType);  //如果要复用，不同于一种样式的listView ,这样的写法是错误的,具体原因看下面第一个小哥评论的说法
                convertView.setTag(R.id.viewHolderCeliangType, viewHoldersecondType);
                break;
            case 2:
                viewHolderChaobiaoType = new ViewHolderChaobiaoType();
                convertView = mLayoutInflater.inflate(R.layout.chaobiao_item, null);
                viewHolderChaobiaoType.check_title_text_view = (TextView) convertView.findViewById(R.id.check_chaobiao_title);
                viewHolderChaobiaoType.check_standard_text_view = (TextView)convertView.findViewById(R.id.check_chaobiao_standard_id);
                viewHolderChaobiaoType.check_danwei_text_view = (TextView)convertView.findViewById(R.id.chaobiao_danwei);
                viewHolderChaobiaoType.show_seismic_id_view = (EditText) convertView.findViewById(R.id.show_chaobiao_text_id);
                try {
                    JSONObject cbInfo = listItem.map.get(position + "");
                    String cddw = cbInfo.getString("cddw");
                    if (cddw.equals("C")) {
                        cddw = " ℃";
                    }
                    viewHolderChaobiaoType.check_title_text_view.setText(cbInfo.getString("description"));
                    viewHolderChaobiaoType.check_standard_text_view.setText(cbInfo.getString("djbz"));
                    viewHolderChaobiaoType.check_danwei_text_view.setText(cddw);
                }catch (JSONException e)
                {

                }

                viewHolderChaobiaoType.check_title_text_view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                        task_activity.jumpToCheckInfoActivity(mPosition);
                    }
                });

                viewHolderChaobiaoType.show_seismic_id_view.setText("");
                try {
                    if(completeDate.getJSONArray("xmdata").getJSONObject(position).getString("sfycd").equals("1"))
                    {
                        viewHolderChaobiaoType.show_seismic_id_view.setTextColor(Color.parseColor("#ff0000"));
                    }
                    viewHolderChaobiaoType.show_seismic_id_view.setText(completeDate.getJSONArray("xmdata").getJSONObject(position).getString("cdjg"));
                } catch (JSONException e) {

                }

                try {
                    if (completeDate.getJSONArray("xmdata").getJSONObject(position).getString("isqx").equals("1")) {
                        convertView.setBackgroundColor(Color.parseColor("#ffc600"));
                    }
                }catch (JSONException e)
                {

                }
                viewHolderChaobiaoType.show_seismic_id_view.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
//                        Log.e("========hasfocus",hasFocus + "");
                        TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                        if (!hasFocus) {
                            // 判 断items的长度是否大于0，可以防止数组越界
                            EditText editText = (EditText)v;
                            if (editText.getText().toString().length() > 0)
                            {
                                task_activity.checkCBxmInfoToCompleteInfo(editText,mPosition,editText.getText().toString());
                            }
                        }
                        else
                        {
                            task_activity.is_jump_to_next = false;
                        }
                    }
                });
//
                break;
        }
        return convertView;
    }

    public void initCheckBoxAction(final CheckBox checkBox, final int position, final String key ){
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TaskCheckActivity task_activity = (TaskCheckActivity) mContext;
                task_activity.setCheckboxChanged(position,checkBox.isChecked(), key);
            }
        });
    }

    class ViewHolderfirstType {
        TextView check_title_text_view;
        TextView check_standard_text_view;
        CheckBox checkBoxPass;
        CheckBox checkBoxDisPass;
    }

    class ViewHoldersecondType {
        TextView check_title_text_view;
        TextView check_standard_text_view;
        Button test_seismic_btn;
        TextView check_danwei_text_view;
        EditText show_seismic_id_view;
    }
    class ViewHolderChaobiaoType {
        TextView check_title_text_view;
        TextView check_standard_text_view;
        TextView check_danwei_text_view;
        EditText show_seismic_id_view;
    }

    public static interface OnViewCompleteListener {
        public void onViewCompleteListener(boolean success);
    }
}
